<template>
  <div class="simplert" role="modal" :class="classSimplert" @click="closeOverlay">
    <div  v-if="type === 'fade_alert'||type === 'fade_success'" class="fadeLert">
      <img v-if="type === 'fade_alert'" :src="require('../dialog/assets/warning.png')" class="pull-left" />
      <img v-if="type === 'fade_success'" :src="require('../dialog/assets/success.png')" class="pull-left" />
      <b v-html="title"></b>
    </div>
    <div v-else class="simplert__content" :class="classContent">
      <div class="simplert__header">
        <div v-if="useIcon">
          <div class="simplert__icon simplert__icon--info" v-if="type === 'info'">
            <div class="simplert__line simplert__line--info">
            </div>
            <div class="simplert__line simplert__line--info-2">
            </div>
          </div>
          <div class="simplert__icon simplert__icon--success" v-if="type === 'success'">
            <div class="simplert__line simplert__line--success">
            </div>
            <div class="simplert__line simplert__line--success-2">
            </div>
          </div>
          <div class="simplert__icon simplert__icon--warning" v-if="type === 'warning'">
            <div class="simplert__line simplert__line--warning">
            </div>
            <div class="simplert__line simplert__line--warning-2">
            </div>
          </div>
          <div class="simplert__icon simplert__icon--error" v-if="type === 'error'">
            <div class="simplert__line simplert__line--error">
            </div>
            <div class="simplert__line simplert__line--error-2">
            </div>
          </div>
          <div class="simplert__icon" v-if="customIconUrl !== ''">
            <img :src="customIconUrl" />
          </div>
        </div>
        <b class="simplert__title" v-html="title"></b>
      </div>
      <div class="simplert__body">
        <div v-if="message" v-html="message"></div>
      </div>
      <div class="divide"></div>
      <div class="simplert__footer">
        <button class="simplert__close" :class="classBtnClose" v-if="useCancelBtn && !hideAllButton" @click="closeSimplert">
          {{ customCloseBtnText }}
        </button>
        <button class="simplert__confirm" v-if="useConfirmBtn && !hideAllButton" :class="classBtnConfirm" @click="whenConfirm">
          {{ customConfirmBtnText }}
        </button>
      </div>
    </div>
  </div>
</template>

<script>
  export default {
    props: {
      useRadius: {
        type: Boolean,
        default: true
      },
      useIcon: {
        type: Boolean,
        default: true
      }
    },

    data: function () {
      return {
        DEFAULT_TYPE: "",
        DEFAULT_BTN_CLOSE_TEXT: "关闭",
        DEFAULT_BTN_CONFIRM_TEXT: "确认",
        INVALID_TYPE: "INVALID_TYPE",
        // hide/show alert
        showSimplert: false,
        // basic setup
        title: "",
        message: "",
        type: this.DEFAULT_TYPE, // info (default), success, warning, error
        customClass: '',
        customIconUrl: '',
        // close button
        customCloseBtnText: this.DEFAULT_BTN_CLOSE_TEXT,
        customCloseBtnClass: '',
        onClose: null,
        // confirm button
        useConfirmBtn: false,
        useCancelBtn: true,
        customConfirmBtnText: this.DEFAULT_BTN_CONFIRM_TEXT,
        customConfirmBtnClass: '',
        onConfirm: null,
        // disabled overlay
        disableOverlayClick: false,
        hideAllButton: false
      }
    },

    computed: {
      classSimplert: function () {
        var clasz = this.customClass
        if (this.showSimplert) {
          clasz = this.customClass + ' simplert--shown'
        }
        return clasz
      },

      classContent: function () {
        var clasz = ''
        if (this.useRadius) {
          clasz = 'simplert__content--radius'
        }
        return clasz
      },

      classBtnClose: function () {
        var clasz = this.customCloseBtnClass
        if (this.useRadius) {
          clasz = ' simplert__close--radius ' + this.customCloseBtnClass
        }
        return clasz
      },

      classBtnConfirm: function () {
        var clasz = this.customConfirmBtnClass
        if (this.useRadius) {
          clasz =  ' simplert__confirm--radius ' + this.customConfirmBtnClass
        }
        return clasz
      }
    },

    methods: {
      closeOverlay: function (e) {
        var _self = this
        if (e.target.className.indexOf('simplert--shown') > 0 && !_self.disableOverlayClick) {
          _self.closeSimplert(e)
        }
      },

      whenConfirm: function (e) {
        var _self = this
        e.preventDefault()

        _self.showSimplert = false

        if (_self.onConfirm !== null) {
          _self.onConfirm()
        }
        this.$emit('confirm')
      },

      closeSimplert: function (e) {
        var _self = this
        e.preventDefault()

        _self.showSimplert = false

        if (_self.onClose !== null) {
          _self.onClose()
        }
      },

      openSimplert: function (obj) {
        var _self = this;
        if (typeof obj !== 'undefined') {
          _self.showSimplert = true;
          _self.title = obj.title;
          if (typeof obj.message !== 'undefined') {
            _self.message = obj.message
          } else {
            _self.message = ''
          }

          if (typeof obj.type !== 'undefined') {
            _self.type = obj.type
          } else {
            _self.type = _self.DEFAULT_TYPE
          }

          if (typeof obj.customClass !== 'undefined') {
            _self.customClass = obj.customClass
          } else {
            _self.customClass = ''
          }

          if (typeof obj.customIconUrl !== 'undefined' && obj.customCloseBtnText !== '') {
            _self.customIconUrl = obj.customIconUrl
            _self.type = _self.INVALID_TYPE
          } else {
            _self.customIconUrl = ''
          }

          // close button setup
          if (typeof obj.customCloseBtnText !== 'undefined' && obj.customCloseBtnText !== '') {
            _self.customCloseBtnText = obj.customCloseBtnText
          } else {
            _self.customCloseBtnText = _self.DEFAULT_BTN_CLOSE_TEXT
          }

          if (typeof obj.customCloseBtnClass !== 'undefined') {
            _self.customCloseBtnClass = obj.customCloseBtnClass
          } else {
            _self.customCloseBtnClass = ''
          }

          if (typeof obj.onClose !== 'undefined' && obj.onClose !== null) {
            _self.onClose = obj.onClose
          } else {
            _self.onClose = null
          }

          // confirm button setup
          if (typeof obj.useConfirmBtn !== 'undefined') {
            _self.useConfirmBtn = obj.useConfirmBtn
          } else {
            _self.useConfirmBtn = false
          }

          if (typeof obj.useCancelBtn !== 'undefined') {
            _self.useCancelBtn = obj.useCancelBtn
          } else {
            _self.useCancelBtn = true
          }

          if (typeof obj.customConfirmBtnText !== 'undefined' && obj.customConfirmBtnText !== '') {
            _self.customConfirmBtnText = obj.customConfirmBtnText
          } else {
            _self.customConfirmBtnText = _self.DEFAULT_BTN_CONFIRM_TEXT
          }

          if (typeof obj.customConfirmBtnClass !== 'undefined') {
            _self.customConfirmBtnClass = obj.customConfirmBtnClass
          } else {
            _self.customConfirmBtnClass = ''
          }

          if (typeof obj.onConfirm !== 'undefined' && obj.onConfirm !== null) {
            _self.onConfirm = obj.onConfirm
          } else {
            _self.onConfirm = null
          }

          if (typeof obj.disableOverlayClick !== 'undefined') {
            _self.disableOverlayClick = obj.disableOverlayClick
          } else {
            _self.disableOverlayClick = false
          }

          if (typeof obj.hideAllButton !== 'undefined') {
            _self.hideAllButton = obj.hideAllButton
          } else {
            _self.hideAllButton = false
          }

          if (obj.type === 'fade_alert' || obj.type === 'fade_success') {
            _self.disableOverlayClick = true;
            if (_self.onClose !== null) {
              _self.onClose()
            }
            setTimeout(function(){
              _self.showSimplert = false
            },2000);
          }
        }
      }
    }
  }
</script>

<style scoped>
  .simplert {
    font-size: 14px;
    line-height: 1.15;
  }

  .simplert {
    position: fixed;
    z-index: 999;
    top: 0;
    left: 0;
    display: none;
    overflow: auto;
    width: 100%;
    height: 100%;
    text-align: center;
    background-color: transparent;
  }

  .simplert--shown {
    display: flex;
    align-items: center;
  }

  .simplert--shown .simplert__content{
    animation-name: zoomIn;
    animation-duration: .3s;
    animation-fill-mode: both;
  }

  .simplert__header {
    padding: 1.5em 0 0;
  }

  .simplert__title {
    font-size: 18px;
    display: block;
  }

  .simplert__content {
    position: relative;
    width: 75%;
    max-width: 400px;
    margin: 0 auto;
    background-color: rgba(255,255,255,0.95);
    padding:0 1px;
    box-shadow: 0 4px 8px 0 rgba(0, 0, 0, 0.2), 0 6px 20px 0 rgba(0, 0, 0, 0.19);
  }

  @media only screen and (min-width: 768px) {
    .simplert__content {
      width: 30%;
    }
  }

  .simplert__content--radius {
    -webkit-border-radius: 0.3em;
    -moz-border-radius: 0.3em;
    border-radius: 0.3em;
  }

  .simplert__body {
    padding-bottom: 1.5em;
  }
  .simplert__body>div{
    padding: 1em 1em 0;
  }

  .simplert__icon {
    position: relative;
    width: 80px;
    height: 80px;
    margin: 10px auto;
    -webkit-border-radius: 50%;
    -moz-border-radius: 50%;
    border-radius: 50%;
  }

  .simplert__icon--info {
    border: 4px solid #c9dae1;
  }

  .simplert__icon--success {
    border: 4px solid #a5dc86;
  }

  .simplert__icon--warning {
    border: 4px solid #f8bb86;
  }

  .simplert__icon--error {
    border: 4px solid #f27474;
  }

  .simplert__icon img {
    width: 80px;
    height: 80px;
  }

  .simplert__line {
    position: absolute;
    z-index: 2;
    display: block;
    height: 5px;
    border-radius: 2px;
  }

  .simplert__line--info {
    position: absolute;
    top: 19px;
    left: 50%;
    width: 7px;
    height: 7px;
    margin-left: -3px;
    border-radius: 50%;
    background-color: #c9dae1;
  }

  .simplert__line--info-2 {
    position: absolute;
    bottom: 15px;
    left: 50%;
    width: 5px;
    height: 29px;
    margin-left: -2px;
    border-radius: 2px;
    background-color: #c9dae1;
  }

  .simplert__line--success {
    top: 44px;
    left: 6px;
    width: 25px;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    background-color: #a5dc86;
  }

  .simplert__line--success-2 {
    top: 38px;
    right: 6px;
    width: 47px;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    background-color: #a5dc86;
  }

  .simplert__line--warning {
    position: absolute;
    top: 10px;
    left: 50%;
    width: 5px;
    height: 35px;
    margin-left: -2px;
    -webkit-border-radius: 2px;
    border-radius: 2px;
    background-color: #f8bb86;
  }

  .simplert__line--warning-2 {
    position: absolute;
    bottom: 10px;
    left: 50%;
    width: 7px;
    height: 7px;
    margin-left: -3px;
    -webkit-border-radius: 50%;
    border-radius: 50%;
    background-color: #f8bb86;
  }

  .simplert__line--error {
    top: 35px;
    left: 13px;
    width: 47px;
    -webkit-transform: rotate(45deg);
    transform: rotate(45deg);
    background-color: #f27474;
  }

  .simplert__line--error-2 {
    top: 35px;
    right: 13px;
    width: 47px;
    -webkit-transform: rotate(-45deg);
    transform: rotate(-45deg);
    background-color: #f27474;
  }

  .simplert__footer {
    position: relative;
    padding: 0;
    display: flex;
    justify-content: center;
  }

  .simplert__close, .simplert__confirm {
    font-size: 16px;
    font-weight: bold;
    display: inline-block;
    padding: 0;
    margin: 1em 0;
    cursor: pointer;
    width: 100%;
    text-align: center;
    color: #ff4610;
    border: none;
    outline: none;
    background: none;
    -webkit-appearance: none;
    -moz-appearance: none;
  }
  .simplert__confirm{ border-left: 1px solid #F0F0F0;}
  .divide{border-top: 1px solid #F0F0F0; margin: 0 1em;}
  .fadeLert{ position: relative;  width: auto; height: auto; margin:0 auto; background-color: rgba(255,255,255,0.95); padding: 0.8em 1em 0.8em 3em; line-height: 1.4; border-radius: 5px; max-width: 90%; text-align: left; box-shadow:0 4px 8px 0 rgba(0, 0, 0, 0.2);}
  .fadeLert>.pull-left{ margin-left: -2em; width: 1.2em;}
  @keyframes zoomIn {
    from {
      transform: scale3d(0.3, 0.3, 0.3);
      opacity: 0;
    }
    50% {
      opacity: 1;
    }
  }
</style>

